﻿using Atlanchim.Crm.Mui.Helpers;
using Lmondeil.Crm.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlanchim.Crm.Mui.Pages.Documents
{
    public class PageViewDocumentModel : AbstractPageViewModel
    {
        /// <summary>
        /// Contact de référence
        /// </summary>
        public Document Document { get; set; }

        /// <summary>
        /// Url d'accès à la page d'édition
        /// </summary>
        public override string EditUrl { get; set; }

        /// <summary>
        /// Chargement
        /// </summary>
        public override void Load()
        {
            // Lecture du fragment
            int idDocument = 0;
            int.TryParse(this.DicFragments["IdDocument"], out idDocument);

            // Chargement
            using (CrmContext context = new CrmContext())
            {
                // Contact
                this.Document = context
                                .Documents
                                .Where(document => document.Id == idDocument)
                                .FirstOrDefault();
                this.EditUrl = string.Format("/Pages/Documents/PageEditDocument.xaml#IdDocument={0}", idDocument);
            }

            // Notification
            OnPropertyChanged("Email");
        }

        /// <summary>
        /// Suppression
        /// </summary>
        protected override void OnDeleteCurrentEntity()
        {
            CrmCrudHelper.DeleteEntityNewContext(this.Document);
        }
    }
}
